<template>
  <div class="login" v-if="codeUrl" style="display: flex;justify-content: center;">
    <img class="login-left-bg" src="../assets/images/login-left-bg.png" alt="">
    <div class="form-content" >
      <div class="form-box">
        <div class="header-box">
          <img src="../assets/images/icon-logo.png" alt="">
          <div class="title">创亿原点</div>
          <div class="sub-title">欢迎使用创亿原点管理后台</div>
        </div>
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
              <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-input
              v-model="loginForm.code"
              auto-complete="off"
              placeholder="验证码"
              style="width: 63%"
              @keyup.enter.native="handleLogin"
            >
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
            <div class="login-code">
              <img :src="codeUrl" @click="getCode" class="login-code-img"/>
            </div>
          </el-form-item>
          <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 15px;">记住密码</el-checkbox>
          <el-form-item class="button-mar-b">
            <el-button
              :loading="loading"
              size="medium"
              type="primary"
              class="login-btn"
              @click.native.prevent="handleLogin"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright ©2024 杭州岩琦网络科技有限公司</span>
    </div> -->
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      loginType:1,
      ddCodeShow: true,
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: "",
				type: 1
      },
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "用户名不能为空" }
        ],
        password: [
          { required: true, trigger: "blur", message: "密码不能为空" }
        ],
        code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  mounted(){
    window.addEventListener('message', this.messageChange);
  },
  beforeDestroy(){
    window.removeEventListener('message', this.messageChange)
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    loginTypeChange(index){
      this.loginType = index
    },
    messageChange(event){
      // 这里的event.data 就是登录成功的信息
      if(event.data && event.data.code){
        this.$store.dispatch("Login", {accountCode:event.data.code,type:2}).then(() => {
          this.$router.push({ path: this.redirect || "/" });
        }).catch(err=>{
          this.ddCodeShow = false
          setTimeout(()=>{
            this.ddCodeShow = true
          })
        })
      };
    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            this.getCode();
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.login {
  height: 100%;
  background: #fff;
  overflow: hidden;
  .login-left-bg{
    width: 60%;
    height: 1080px;
  }
  .form-content{
    display: flex;align-items: center;justify-content: center; width: 40%;
  }
}
.header-box{
  text-align: center;
  margin-bottom: 25px;
  .title {
    letter-spacing: 3px;
    font-weight: bold;
    font-size: 20px;
    margin: 10px 0 5px;
  }
  .sub-title{
    color: #aaa;
  }
  img{
    width: 48px;
    height: 48px;
  }
}
.form-box{
  align-items: center;
  position: absolute;
  // right: 20%;
  top: 50%;
  transform: translateY(-50%);
  background: #ffffff;
  box-shadow: 0 0 10px #e5e5e5;
  border-radius: 8px;
  padding: 50px 60px 0;
  width: 439px;
  height: 500px;
}
.qrcode-box{
  width: 400px;
  overflow: hidden;
  iframe{
    width: 320px;
    height: 327px;
    margin-top: -80px;
  }
}
.login-form {
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.login-btn {
  padding: 13px 20px;
  font-size: 16px;
  border-radius: 50px;
  width: 100%;
}
.login-code-img {
  height: 38px;
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #333;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
// @media (max-width: 1720px) and (min-width: 1620px) {
//   .form-box{
//     right: 12%;
//   }
// }
// @media (max-width: 1620px) and (min-width: 1520px) {
//   .form-box{
//     right: 10%;
//   }
// }
// @media (max-width: 1520px) and (min-width: 1420px) {
//   .form-box{
//     right: 8%;
//   }
// }
// @media (max-width: 1420px) and (min-width: 1320px) {
//   .form-box{
//     right: 6%;
//   }
// }
// @media (max-width: 1320px) and (min-width: 1220px) {
//   .form-box{
//     right: 4%;
//   }
// }
// @media (max-width: 1220px) and (min-width: 1020px) {
//   .form-box{
//     right: 0%;
//   }
// }
// @media (max-width: 758px){
//   .form-box{
//     transform: translateX(50%) translateY(-50%);
//     right: 50%;
//   }
// }
</style>